import adminsApi from "../../api/admins.js";
import Base from '../base.js';
// 暴露出去
export default class extends Base {
  render() {
    this.$el.html(`
   
    <section class="main" id="loginEle">
    <div class="logo text-center">
        <h1> <a href="index.html"> 猫眼后台管理</a></h1>
    </div>
    <div class="content-w3ls text-center">
    <a href="#/usersLogin"><div class="img-responsive"></div></a>
        <form action="#" method="post">
            <div class="wthree-field">
                <input name="username" id="text1" type="text" value="admin" placeholder="账号" required>
            </div>
            <div class="wthree-field">
                <input name="password" id="myInput" type="Password" value="123" placeholder="密码">
            </div>
            <div class="wthree-field">
                <button type="button" class="btn" name="login-btn">登录</button>
            </div>
            <div class="wthree-field">
              
                <button class="btn"  name="reg-btn" type="button">注册</button>
            </div>
            <div class="login-bottom">
                <a href="https://www.csdn.net" class="">help</a>
            </div>
        </form>
    </div>
    <div class="copyright">
        <p>©2021 Four groups<a href="https://w3layouts.com" target="_blank">The tail</a></p>
    </div>
</section>
    `);
    this.$usernameEle = $("input[name=username]");
    this.$passwordEle = $("input[name=password]");
    this.$loginBtn = $("button[name=login-btn]");
    this.$regBtn = $("button[name=reg-btn]");
  }
  async loginHandle() { //登录
    const adminsname = this.$usernameEle.val(); //获取值
    const adminspassword = this.$passwordEle.val();
    // location.hash="#/info";// 视图跳转
    const  data  = await adminsApi.adminsLogin({
      adminsname,
      adminspassword,
    });
    
    if (data) {
      // adminsApi.setToken(token);
      location.hash = "#/info";
      return;
    }
        // 登录失败弹窗
        layer.msg(`登录失败，5秒后自动关闭`, {
          time: 5*1000
          ,success: function (layero, index){
            var timeNum = this.time/1000, setText = function(start){
              layer.title((start ? timeNum : --timeNum) + ' 秒后关闭', index);
            };
            setText(!0);
            this.timer = setInterval(setText, 1000);
            if(timeNum <= 0) clearInterval(this.timer);
          }
          ,end: function(){
            clearInterval(this.timer);
          },
          btn: ['确&nbsp&nbsp&nbsp&nbsp定','返&nbsp&nbsp&nbsp&nbsp回']
        });  
  };
  regHandle() {
    location.hash = "#/reg" //视图跳转注册页面
  };
  handle() {
    this.$loginBtn.on("click", this.loginHandle.bind(this));
    this.$regBtn.on("click", this.regHandle.bind(this));
  };
}

